function table=lookup_table(type,quantization,width,twos_en,module,outputDir)
    num_entries=2^width;
    
    if(nargin<4)
        twos_en=0;
    end
    
    if(twos_en)
        max_value=2^(quantization-1)-1;
    else
        max_value=2^quantization-1;
    end
    
    if(strcmpi(type,'sin'))
        if(twos_en)
            table=round(max_value*(sin(linspace(0,2*pi,num_entries))'));
        else
            table=round(max_value*(sin(linspace(0,2*pi,num_entries))'+1)/2);
        end
    elseif(strcmpi(type,'cos'))
        if(twos_en)
            table=round(max_value*(cos(linspace(0,2*pi,num_entries))'));
        else
            table=round(max_value*(cos(linspace(0,2*pi,num_entries))'/.5+.5));
        end
    elseif(strcmpi(type,'tri'))
        if(twos_en)
            a=linspace(0,1,num_entries/4+1);
            b=linspace(1,-1,num_entries/2);
            c=linspace(-1,0,num_entries/4+1);
        else
            a=linspace(0.5,1,num_entries/4+1);
            b=linspace(1,0,num_entries/2);
            c=linspace(0,0.5,num_entries/4+1);
        end
        x=[a(1:end-1) b c(2:end)];
        table=round(max_value*x');%FIXME
    elseif(strcmpi(type,'saw'))
        if(twos_en)
            table=round(max_value*(linspace(-1,1,num_entries)'));
        else
            table=round(max_value*(linspace(0,1,num_entries)'));
        end
    else
        table=zeros(num_entries,1);
    end
    
    if(twos_en)
        negative=table<0;
        table=abs(table);
        table=bitxor(table,2^(quantization-1)*negative);
    end
    
    if(nargin>4)
        if(nargin<6)
            outputDir='.';
        elseif(outputDir(end)=='/')
            outputDir=outputDir(1:end-1);
        end
        
        const=upper(module);
        
        %Print header definition file.
        f=fopen(sprintf('%s/%s.csv',outputDir,module),'wt');
        
        fprintf(f,'//This file was automatically generated by\n');
        fprintf(f,'//Matlab on %s.\n',datestr(now));
        fprintf(f,'\n');
        fprintf(f,'%s_INPUT_WIDTH,%d,%s lookup table input width.\n',const,width,module);
        fprintf(f,'%s_INPUT_RANGE,%s_INPUT_WIDTH-1:0,%s lookup table input bit range.\n',const,const,module);
        fprintf(f,'\n');
        fprintf(f,'%s_OUTPUT_WIDTH,%d,%s lookup table output width.\n',const,quantization,module);
        fprintf(f,'%s_OUTPUT_RANGE,%s_OUTPUT_WIDTH-1:0,%s lookup table output bit range.\n',const,const,module);
        
        fclose(f);
        
        %Print module.
        f=fopen(sprintf('%s/%s.v',outputDir,module),'wt');
        
        fprintf(f,'//This file was automatically generated by\n');
        fprintf(f,'//Matlab on %s.\n',datestr(now));
        fprintf(f,'`include "%s"\n',sprintf('%s.vh',module));
        fprintf(f,'\n');
        
        fprintf(f,'module %s(\n',module);
        fprintf(f,'    input [`%s_INPUT_RANGE]       in,\n',const);
        fprintf(f,'    output reg [`%s_OUTPUT_RANGE] out);\n',const);
        fprintf(f,'\n');
        fprintf(f,'   always @(in) begin\n');
        fprintf(f,'     casez(in)\n');
        for i=1:num_entries
          fprintf(f,'       `%s_INPUT_WIDTH\''d%d: out <= `%s_OUTPUT_WIDTH\''d%d;\n',...
                const,i-1,...
                const,table(i));
        end
        fprintf(f,'       default: out <= `%s_OUTPUT_WIDTH\''hx;\n',const);
        fprintf(f,'     endcase\n');
        fprintf(f,'   end\n');
        fprintf(f,'endmodule\n');
        
        fclose(f);
    end
end